package Frame.librarianFrame;

import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import Frame.common.TableBackGround;
import controller.Controller;
import table.BarCodeAndBook;
import table.Book;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.Point;
//import sun.swing.DefaultLookup;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * @author 陈思威 图书借阅界面 共有 当其他用户调用该界面时，用remove方法删除指定的组件，即更换顶部的功能按钮
 *
 * 
 */
public class BookBorrowFrame extends JFrame {

	private JPanel contentPane;
	private JPanel panel1;
	private JScrollPane borrowInformationTable;
	private JTextField tf1;
	private JTextField tf2;
	private JButton bt3;
	private JLabel lb3;
	private JLabel lb7;
	private JLabel lb5;
	private JLabel lb9;
	private JLabel lb4;
	private JLabel lb6;
	private JLabel lb8;
	private JLabel lb10;
	private JTable borrowInformation;
	private Controller controller;
	public BookBorrowFrame(Controller controller) {
		this.controller = controller;
		//controller.setBookBorrowFrame(this); 
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.setSize(1920, 971);
		contentPane.add(JPanel());
		//先添加一个空链表的表格 因为表格查询结果当中用到了remove() 否则会null point
		contentPane.add(getBorrowTablePanel(controller.getLibrarianService().getBookByBarCode(1000)));	
		contentPane.add(getBBt10());
		contentPane.add(getBBt11());
	}
	public JButton getBBt10() {
		JButton bt10 = new JButton("借阅");
		bt10.setBounds(691, 801, 119, 35);
		bt10.setFont(new Font("华文楷体", Font.PLAIN, 20));
		bt10.addActionListener(controller);
		bt10.setActionCommand("bookBorrow");
		return bt10;
	}
	public JButton getBBt11() {
		JButton bt11 = new JButton("归还");
		bt11.setFont(new Font("华文楷体", Font.PLAIN, 20));
		bt11.addActionListener(controller);
		bt11.setActionCommand("bookReturn");
		bt11.setBounds(1065, 801, 119, 35);
		return bt11;
	}
	/**
	 * @return 图书借阅界面 通过读者账号 查询读者的 姓名 身份 性别 可借数量
	 */
	public JPanel JPanel() {
		panel1 = new JPanel();
		panel1.setBackground(Color.WHITE);
		panel1.setLocation(0, 0);
		panel1.setSize(1920, 278);
		panel1.setLayout(null);

		JLabel lb1 = new JLabel("用户名:");
		lb1.setFont(new Font("华文楷体", Font.PLAIN, 20));
		lb1.setBounds(163, 72, 64, 27);
		panel1.add(lb1);

		tf1 = new JTextField();
		tf1.setFont(new Font("华文楷体", Font.PLAIN, 20));
		tf1.setColumns(10);
		tf1.setBounds(244, 72, 171, 31);
		panel1.add(tf1);

		JLabel lb2 = new JLabel("图书编号:");
		lb2.setFont(new Font("华文楷体", Font.PLAIN, 20));
		lb2.setBounds(143, 152, 90, 27);
		panel1.add(lb2);

		tf2 = new JTextField();
		tf2.setFont(new Font("华文楷体", Font.PLAIN, 20));
		tf2.setColumns(10);
		tf2.setBounds(244, 150, 171, 31);
		panel1.add(tf2);

		bt3 = new JButton("查询");
		bt3.setIcon(new ImageIcon(BookBorrowFrame.class.getResource("/logo/搜索.png")));
		bt3.setFont(new Font("华文楷体", Font.PLAIN, 20));
		bt3.addActionListener(controller);
		bt3.setActionCommand("BorrowFindUser");
		bt3.setBounds(265, 217, 119, 35);
		panel1.add(bt3);

		lb3 = new JLabel("用户名:");
		lb3.setFont(new Font("华文楷体", Font.PLAIN, 20));
		lb3.setBounds(901, 72, 64, 27);
		panel1.add(lb3);

		lb7 = new JLabel("已借数量:");
		lb7.setFont(new Font("华文楷体", Font.PLAIN, 20));
		lb7.setBounds(1383, 72, 90, 27);
		panel1.add(lb7);

		lb5 = new JLabel("身份:");
		lb5.setFont(new Font("华文楷体", Font.PLAIN, 20));
		lb5.setBounds(911, 185, 55, 27);
		panel1.add(lb5);

		lb9 = new JLabel("可借数量:");
		lb9.setFont(new Font("华文楷体", Font.PLAIN, 20));
		lb9.setBounds(1383, 185, 90, 27);
		panel1.add(lb9);

		lb4 = new JLabel("");
		lb4.setFont(new Font("华文楷体", Font.PLAIN, 20));
		lb4.setBounds(985, 72, 171, 27);
		panel1.add(lb4);

		lb6 = new JLabel("");
		lb6.setFont(new Font("华文楷体", Font.PLAIN, 20));
		lb6.setBounds(979, 185, 171, 27);
		panel1.add(lb6);

		lb8 = new JLabel("");
		lb8.setFont(new Font("华文楷体", Font.PLAIN, 20));
		lb8.setBounds(1487, 72, 171, 27);
		panel1.add(lb8);

		lb10 = new JLabel();
		lb10.setFont(new Font("华文楷体", Font.PLAIN, 20));
		lb10.setBounds(1487, 185, 171, 27);
		panel1.add(lb10);

		return panel1;
	}
	/**
	 * @return 图书借阅界面 通过图书编号 查看的图书相关信息
	 */
	public JScrollPane getBorrowTablePanel(List<BarCodeAndBook> barCodeAndBookList) {
		Object dataName[] = { "书名", "作者", "出版社", "索书号", "价格", "ISBN", "编号","图书状态"};
		Object data[][] = new Object[barCodeAndBookList.size()][8];
		Iterator<BarCodeAndBook> iter = barCodeAndBookList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			BarCodeAndBook bar = iter.next();
			data[i][0] = bar.getBookName();  
			data[i][1] = bar.getBookWriter();
			data[i][2] = bar.getBookPublishingHouse();
			data[i][3] = bar.getBookCallNumber();
			data[i][4] = bar.getBookPrice();			
			data[i][5] = bar.getBookIsbn();
			data[i][6] = bar.getBarCodeId();
			data[i][7] = bar.getBookStatus();
			i++;
		} 	
		borrowInformation = new JTable(data, dataName) {
			@Override
		    public Component prepareRenderer(TableCellRenderer renderer, int row, int column){//设置鼠标光标划过时的颜色改变
	        Component comp = super.prepareRenderer(renderer,row,column);
		        Point p = getMousePosition();
		    	
		        if(p!=null){
		            int rowUnderMouse = rowAtPoint(p);
		            if(rowUnderMouse == row){
		                  comp.setBackground(new Color(223,240,216));
		           }
//		            else{
//		                  comp.setBackground(DefaultLookup.getColor(this,ui,"Table.alternateRowColor"));
//		           }
		        }		         
		        return comp;
		    }
			private static final long serialVersionUID = 1L;
			public boolean isCellEditable(int row, int column) {
				return false; 
			};// 将表设置成无法修改
			
		}; 
		borrowInformation.addMouseMotionListener(controller.getMyTableMouseMotion());
		borrowInformation.setSelectionBackground(new Color(223,240,216));//设置选中的行的颜色
		TableBackGround.setColumnColor(borrowInformation);//设置表的交替行数的颜色
		borrowInformation.getTableHeader().setFont(new Font("华文楷体", Font.PLAIN, 20)); // 表头字体
		borrowInformation.setFont(new Font("华文楷体", Font.PLAIN, 21)); // 表体字体
		borrowInformation.getTableHeader().setBackground(new Color(184,207,240));//设置表头的颜色
		borrowInformation.getTableHeader().setPreferredSize(new Dimension(15, 35)); // 表头高度 先后为宽 高
		DefaultTableCellRenderer cr = new DefaultTableCellRenderer();// 将表体的内容设置为居中显示
		borrowInformation.setDefaultRenderer(Object.class, cr);
		borrowInformation.setRowHeight(borrowInformation.getRowHeight() + 15); // 表体高度
		borrowInformation.setRowMargin(5);// 设置相邻行中的单元格之间的空白空间量
		//borrowInformation.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 表格只能选择一行
		borrowInformation.repaint();
		borrowInformationTable = new JScrollPane(borrowInformation);
		borrowInformationTable.getViewport().setBackground(Color.WHITE);//将JScrollPane设置成白色
		borrowInformationTable.setLocation(0, 277);
		borrowInformationTable.setSize(1920, 489);	
		return borrowInformationTable;
	}
	public void showSearchResult(List<BarCodeAndBook> result) {
		contentPane.remove(borrowInformationTable);
		contentPane.add(getBorrowTablePanel(result));
		contentPane.validate();
	}
	public void refresh() {
		contentPane.remove(borrowInformationTable);
		contentPane.add(getBorrowTablePanel(controller.getLibrarianService().getBookByBarCode(1000)));
		contentPane.validate();
	}
	public JPanel getContentPane() { 
		return contentPane;
	}

	public void setContentPane(JPanel contentPane) {
		this.contentPane = contentPane;
	}

	public JTextField getTf1() {
		return tf1;
	}

	public void setTf1(JTextField tf1) {
		this.tf1 = tf1;
	}

	public JTextField getTf2() {
		return tf2;
	}

	public void setTf2(JTextField tf2) {
		this.tf2 = tf2;
	}

	public JLabel getLb4() {
		return lb4;
	}

	public void setLb4(JLabel lb4) {
		this.lb4 = lb4;
	}

	public JLabel getLb6() {
		return lb6;
	}

	public void setLb6(JLabel lb6) {
		this.lb6 = lb6;
	}

	public JLabel getLb8() {
		return lb8;
	}

	public void setLb8(JLabel lb8) {
		this.lb8 = lb8;
	}

	public JLabel getLb10() {
		return lb10;
	}

	public void setLb10(JLabel lb10) {
		this.lb10 = lb10;
	}

	public JScrollPane getBorrowInformationTable() {
		return borrowInformationTable;
	}

	public void setBorrowInformationTable(JScrollPane borrowInformationTable) {
		this.borrowInformationTable = borrowInformationTable;
	}
}
